﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Ionic.Zip;
using System.IO;
using System.IO.Compression;

/// <summary>
/// Summary description for AfitFile
/// </summary>
namespace PTS.Service
{
    public class AfitFile
    {
        public AfitFile()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static void ZipCompressFile(string source, string zipFile)
        {
            using (FileStream fs = new FileStream(zipFile, FileMode.Create, FileAccess.Write))
            using (GZipStream gzs = new GZipStream(fs, CompressionMode.Compress))
            {
                byte[] data = File.ReadAllBytes(source);
                gzs.Write(data, 0, data.Length);
                gzs.Flush();
            }
        }

        public static void ZipDecompressFile(string zipFile, string originalFile)
        {
            using (FileStream fsZip = new FileStream(zipFile, FileMode.Open, FileAccess.Read))
            using (FileStream fsOriginal = new FileStream(originalFile, FileMode.Create, FileAccess.Write))
            using (GZipStream gzs = new GZipStream(fsZip, CompressionMode.Decompress))
            {
                while (true)
                {
                    byte[] buffer = new byte[100];
                    int bytesRead = gzs.Read(buffer, 0, buffer.Length);
                    fsOriginal.Write(buffer, 0, bytesRead);
                    if (bytesRead != buffer.Length)
                    {
                        break;
                    }
                }
            }
        }
        public static void ZipCompressFolder(string sourceFile, string zipFile)
        {
            try
            {
                using (ZipFile zip = new ZipFile())
                {
                    zip.StatusMessageTextWriter = System.Console.Out;
                    zip.AddDirectory(sourceFile); // recurses subdirectories
                    zip.Save(zipFile);
                }
            }
            catch (System.Exception ex)
            {
                System.Console.Error.WriteLine("exception: " + ex);
            }
        }
        public static void ZipDeCompressFolder(string zipFile, string destFile)
        {
            try
            {
                using (ZipFile zip = ZipFile.Read(zipFile))
                {
                    zip.ExtractAll(destFile, ExtractExistingFileAction.DoNotOverwrite);
                }
            }
            catch (System.Exception ex)
            {
                System.Console.Error.WriteLine("exception: " + ex);
                throw ex;
            }
        }
        public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = false;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                }
                ret = true;
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }
        public static bool DeleteFolder(string path)
        {
            try
            {
                System.IO.DirectoryInfo ptsFolder = new DirectoryInfo(path);
                if (ptsFolder.Exists)
                {
                    foreach (FileInfo file in ptsFolder.GetFiles())
                    {
                        file.Delete();
                    }
                    foreach (DirectoryInfo dir in ptsFolder.GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }
                if (Directory.Exists(path))
                    Directory.Delete(path);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool DeleteFile(string path)
        {
            try
            {
                if (File.Exists(path))
                    File.Delete(path);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
